/*
 * DIM-SUM操作系统 -- 计算数据结构偏移，用于汇编代码
 *
 * Copyright (C) 2023 国科础石(重庆)软件有限公司
 *
 * 作者: Dong Peng <w-pengdong@kernelsoft.com>
 *
 * License terms: GNU General Public License (GPL) version 3
 *
 */

#include <linux/compiler.h>
#include <dim-sum/sched.h>
#include <dim-sum/kbuild.h>
#include <dim-sum/page_area.h>
#include <dim-sum/irq.h>
#include <dim-sum/irqflags.h>
#include <dim-sum/mm_types.h>
#include <dim-sum/mm.h>
#include <dim-sum/process.h>
#include <asm/page.h>
int main(void)
{
	DEFINE(THREAD_START_SP, (PROCESS_STACK_SIZE - 16));
	DEFINE(__TIF_SYSCALL_TRACE, _TIF_SYSCALL_TRACE);
	DEFINE(__TIF_NEED_RESCHED, _TIF_NEED_RESCHED);
	DEFINE(__TIF_WORK_MASK, _TIF_WORK_MASK);
	DEFINE(__TIF_SINGLESTEP, PROCFLAG_SINGLESTEP);
	DEFINE(S_FRAME_SIZE, sizeof(struct exception_spot));

	OFFSET(TASK_SPOT_RA, task_desc, task_spot.cpu_context.pc);
	OFFSET(TASK_SPOT_SP, task_desc, task_spot.cpu_context.sp);
	OFFSET(TASK_SPOT_S0, task_desc, task_spot.cpu_context.s[0]);
	OFFSET(TASK_SPOT_S1, task_desc, task_spot.cpu_context.s[1]);
	OFFSET(TASK_SPOT_S2, task_desc, task_spot.cpu_context.s[2]);
	OFFSET(TASK_SPOT_S3, task_desc, task_spot.cpu_context.s[3]);
	OFFSET(TASK_SPOT_S4, task_desc, task_spot.cpu_context.s[4]);
	OFFSET(TASK_SPOT_S5, task_desc, task_spot.cpu_context.s[5]);
	OFFSET(TASK_SPOT_S6, task_desc, task_spot.cpu_context.s[6]);
	OFFSET(TASK_SPOT_S7, task_desc, task_spot.cpu_context.s[7]);
	OFFSET(TASK_SPOT_S8, task_desc, task_spot.cpu_context.s[8]);
	OFFSET(TASK_SPOT_S9, task_desc, task_spot.cpu_context.s[9]);
	OFFSET(TASK_SPOT_S10, task_desc, task_spot.cpu_context.s[10]);
	OFFSET(TASK_SPOT_S11, task_desc, task_spot.cpu_context.s[11]);
	OFFSET(TASK_SPOT_SP, task_desc, task_spot.cpu_context.sp);
	OFFSET(TASK_STACK, task_desc, stack);
	// OFFSET(TASK_TI, task_desc, thread_info);
	OFFSET(TASK_TI_FLAGS, task_desc, flags);
	OFFSET(TASK_TI_KERNEL_SP, process_desc, arch_desc.kernel_sp);
	OFFSET(TASK_TI_USER_SP, process_desc, arch_desc.user_sp);
	OFFSET(TASK_TI_REGS_SP, process_desc, arch_desc.regs_sp);

	OFFSET(TASK_SPOT_F0,  task_desc, task_spot.cpu_context.fstate.f[0]);
	OFFSET(TASK_SPOT_F1,  task_desc, task_spot.cpu_context.fstate.f[1]);
	OFFSET(TASK_SPOT_F2,  task_desc, task_spot.cpu_context.fstate.f[2]);
	OFFSET(TASK_SPOT_F3,  task_desc, task_spot.cpu_context.fstate.f[3]);
	OFFSET(TASK_SPOT_F4,  task_desc, task_spot.cpu_context.fstate.f[4]);
	OFFSET(TASK_SPOT_F5,  task_desc, task_spot.cpu_context.fstate.f[5]);
	OFFSET(TASK_SPOT_F6,  task_desc, task_spot.cpu_context.fstate.f[6]);
	OFFSET(TASK_SPOT_F7,  task_desc, task_spot.cpu_context.fstate.f[7]);
	OFFSET(TASK_SPOT_F8,  task_desc, task_spot.cpu_context.fstate.f[8]);
	OFFSET(TASK_SPOT_F9,  task_desc, task_spot.cpu_context.fstate.f[9]);
	OFFSET(TASK_SPOT_F10, task_desc, task_spot.cpu_context.fstate.f[10]);
	OFFSET(TASK_SPOT_F11, task_desc, task_spot.cpu_context.fstate.f[11]);
	OFFSET(TASK_SPOT_F12, task_desc, task_spot.cpu_context.fstate.f[12]);
	OFFSET(TASK_SPOT_F13, task_desc, task_spot.cpu_context.fstate.f[13]);
	OFFSET(TASK_SPOT_F14, task_desc, task_spot.cpu_context.fstate.f[14]);
	OFFSET(TASK_SPOT_F15, task_desc, task_spot.cpu_context.fstate.f[15]);
	OFFSET(TASK_SPOT_F16, task_desc, task_spot.cpu_context.fstate.f[16]);
	OFFSET(TASK_SPOT_F17, task_desc, task_spot.cpu_context.fstate.f[17]);
	OFFSET(TASK_SPOT_F18, task_desc, task_spot.cpu_context.fstate.f[18]);
	OFFSET(TASK_SPOT_F19, task_desc, task_spot.cpu_context.fstate.f[19]);
	OFFSET(TASK_SPOT_F20, task_desc, task_spot.cpu_context.fstate.f[20]);
	OFFSET(TASK_SPOT_F21, task_desc, task_spot.cpu_context.fstate.f[21]);
	OFFSET(TASK_SPOT_F22, task_desc, task_spot.cpu_context.fstate.f[22]);
	OFFSET(TASK_SPOT_F23, task_desc, task_spot.cpu_context.fstate.f[23]);
	OFFSET(TASK_SPOT_F24, task_desc, task_spot.cpu_context.fstate.f[24]);
	OFFSET(TASK_SPOT_F25, task_desc, task_spot.cpu_context.fstate.f[25]);
	OFFSET(TASK_SPOT_F26, task_desc, task_spot.cpu_context.fstate.f[26]);
	OFFSET(TASK_SPOT_F27, task_desc, task_spot.cpu_context.fstate.f[27]);
	OFFSET(TASK_SPOT_F28, task_desc, task_spot.cpu_context.fstate.f[28]);
	OFFSET(TASK_SPOT_F29, task_desc, task_spot.cpu_context.fstate.f[29]);
	OFFSET(TASK_SPOT_F30, task_desc, task_spot.cpu_context.fstate.f[30]);
	OFFSET(TASK_SPOT_F31, task_desc, task_spot.cpu_context.fstate.f[31]);
	OFFSET(TASK_SPOT_FCSR, task_desc, task_spot.cpu_context.fstate.fcsr);

	DEFINE(ESPOT_SIZE, sizeof(struct exception_spot));
	OFFSET(ESPOT_SEPC, exception_spot, sepc);
	OFFSET(ESPOT_RA, exception_spot, ra);
	OFFSET(ESPOT_FP, exception_spot, s0);
	OFFSET(ESPOT_S0, exception_spot, s0);
	OFFSET(ESPOT_S1, exception_spot, s1);
	OFFSET(ESPOT_S2, exception_spot, s2);
	OFFSET(ESPOT_S3, exception_spot, s3);
	OFFSET(ESPOT_S4, exception_spot, s4);
	OFFSET(ESPOT_S5, exception_spot, s5);
	OFFSET(ESPOT_S6, exception_spot, s6);
	OFFSET(ESPOT_S7, exception_spot, s7);
	OFFSET(ESPOT_S8, exception_spot, s8);
	OFFSET(ESPOT_S9, exception_spot, s9);
	OFFSET(ESPOT_S10, exception_spot, s10);
	OFFSET(ESPOT_S11, exception_spot, s11);
	OFFSET(ESPOT_SP, exception_spot, sp);
	OFFSET(ESPOT_TP, exception_spot, tp);
	OFFSET(ESPOT_A0, exception_spot, a0);
	OFFSET(ESPOT_A1, exception_spot, a1);
	OFFSET(ESPOT_A2, exception_spot, a2);
	OFFSET(ESPOT_A3, exception_spot, a3);
	OFFSET(ESPOT_A4, exception_spot, a4);
	OFFSET(ESPOT_A5, exception_spot, a5);
	OFFSET(ESPOT_A6, exception_spot, a6);
	OFFSET(ESPOT_A7, exception_spot, a7);
	OFFSET(ESPOT_T0, exception_spot, t0);
	OFFSET(ESPOT_T1, exception_spot, t1);
	OFFSET(ESPOT_T2, exception_spot, t2);
	OFFSET(ESPOT_T3, exception_spot, t3);
	OFFSET(ESPOT_T4, exception_spot, t4);
	OFFSET(ESPOT_T5, exception_spot, t5);
	OFFSET(ESPOT_T6, exception_spot, t6);
	OFFSET(ESPOT_GP, exception_spot, gp);
	OFFSET(ESPOT_ORIG_A0, exception_spot, orig_a0);
	OFFSET(ESPOT_SSTATUS, exception_spot, sstatus);
	OFFSET(ESPOT_SBADADDR, exception_spot, sbadaddr);
	OFFSET(ESPOT_SCAUSE, exception_spot, scause);

	/*
	 * THREAD_{F,X}* might be larger than a S-type offset can handle, but
	 * these are used in performance-sensitive assembly so we can't resort
	 * to loading the long immediate every time.
	 */
	DEFINE(TASK_SPOT_RA_RA,
		  offsetof(struct task_desc, task_spot.cpu_context.pc)
		- offsetof(struct task_desc, task_spot.cpu_context.pc)
	);
	DEFINE(TASK_SPOT_SP_RA,
		  offsetof(struct task_desc, task_spot.cpu_context.sp)
		- offsetof(struct task_desc, task_spot.cpu_context.pc)
	);
	DEFINE(TASK_SPOT_S0_RA,
		  offsetof(struct task_desc, task_spot.cpu_context.s[0])
		- offsetof(struct task_desc, task_spot.cpu_context.pc)
	);
	DEFINE(TASK_SPOT_S1_RA,
		  offsetof(struct task_desc, task_spot.cpu_context.s[1])
		- offsetof(struct task_desc, task_spot.cpu_context.pc)
	);
	DEFINE(TASK_SPOT_S2_RA,
		  offsetof(struct task_desc, task_spot.cpu_context.s[2])
		- offsetof(struct task_desc, task_spot.cpu_context.pc)
	);
	DEFINE(TASK_SPOT_S3_RA,
		  offsetof(struct task_desc, task_spot.cpu_context.s[3])
		- offsetof(struct task_desc, task_spot.cpu_context.pc)
	);
	DEFINE(TASK_SPOT_S4_RA,
		  offsetof(struct task_desc, task_spot.cpu_context.s[4])
		- offsetof(struct task_desc, task_spot.cpu_context.pc)
	);
	DEFINE(TASK_SPOT_S5_RA,
		  offsetof(struct task_desc, task_spot.cpu_context.s[5])
		- offsetof(struct task_desc, task_spot.cpu_context.pc)
	);
	DEFINE(TASK_SPOT_S6_RA,
		  offsetof(struct task_desc, task_spot.cpu_context.s[6])
		- offsetof(struct task_desc, task_spot.cpu_context.pc)
	);
	DEFINE(TASK_SPOT_S7_RA,
		  offsetof(struct task_desc, task_spot.cpu_context.s[7])
		- offsetof(struct task_desc, task_spot.cpu_context.pc)
	);
	DEFINE(TASK_SPOT_S8_RA,
		  offsetof(struct task_desc, task_spot.cpu_context.s[8])
		- offsetof(struct task_desc, task_spot.cpu_context.pc)
	);
	DEFINE(TASK_SPOT_S9_RA,
		  offsetof(struct task_desc, task_spot.cpu_context.s[9])
		- offsetof(struct task_desc, task_spot.cpu_context.pc)
	);
	DEFINE(TASK_SPOT_S10_RA,
		  offsetof(struct task_desc, task_spot.cpu_context.s[10])
		- offsetof(struct task_desc, task_spot.cpu_context.pc)
	);
	DEFINE(TASK_SPOT_S11_RA,
		  offsetof(struct task_desc, task_spot.cpu_context.s[11])
		- offsetof(struct task_desc, task_spot.cpu_context.pc)
	);

	DEFINE(TASK_SPOT_F0_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F1_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[1])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F2_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[2])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F3_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[3])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F4_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[4])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F5_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[5])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F6_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[6])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F7_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[7])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F8_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[8])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F9_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[9])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F10_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[10])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F11_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[11])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F12_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[12])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F13_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[13])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F14_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[14])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F15_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[15])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F16_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[16])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F17_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[17])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F18_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[18])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F19_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[19])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F20_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[20])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F21_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[21])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F22_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[22])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F23_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[23])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F24_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[24])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F25_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[25])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F26_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[26])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F27_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[27])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F28_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[28])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F29_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[29])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F30_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[30])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_F31_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.f[31])
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);
	DEFINE(TASK_SPOT_FCSR_F0,
		  offsetof(struct task_desc, task_spot.cpu_context.fstate.fcsr)
		- offsetof(struct task_desc, task_spot.cpu_context.fstate.f[0])
	);

	/* The assembler needs access to THREAD_SIZE as well. */
	DEFINE(ASM_THREAD_SIZE, THREAD_SIZE);

	/*
	 * We allocate a exception_spot on the stack when entering the kernel.  This
	 * ensures the alignment is sane.
	 */
	DEFINE(ESPOT_SIZE_ON_STACK, ALIGN(sizeof(struct exception_spot), STACK_ALIGN));
}
